package com.itfreer.utils.json;

import java.io.IOException;
import java.util.List;

import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.DeserializationConfig;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.SerializationConfig;
import org.codehaus.jackson.map.annotate.JsonSerialize;
import org.codehaus.jackson.type.JavaType;

/**
 * 
 */
public class JsonUtils {

	/***
	 * 将类型转换为字符串
	 * 
	 * @throws IOException
	 * @throws JsonMappingException
	 * @throws JsonGenerationException
	 */
	public static <T> String toJSONString(List<T> list) {
		ObjectMapper mapper = new ObjectMapper();
		// 过滤对象的null属性.
		mapper.setSerializationInclusion(JsonSerialize.Inclusion.NON_NULL);
		// 过滤map中的null值
		mapper.configure(SerializationConfig.Feature.WRITE_NULL_MAP_VALUES, false);

		try {
			return mapper.writeValueAsString(list);
		} catch (IOException e) {
			e.printStackTrace();
			return "";
		}
	}

	/***
	 * 将类型转换为字符串
	 * 
	 * @param object
	 * @return
	 * @throws IOException
	 * @throws JsonMappingException
	 * @throws JsonGenerationException
	 */
	public static String toJSONString(Object object) {
		ObjectMapper mapper = new ObjectMapper();
		// 过滤对象的null属性.
		mapper.setSerializationInclusion(JsonSerialize.Inclusion.NON_NULL);
		// 过滤map中的null值
		mapper.configure(SerializationConfig.Feature.WRITE_NULL_MAP_VALUES, false);
		try {
			return mapper.writeValueAsString(object);
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * 将JSON转换为对象
	 * 
	 * @param <T>
	 * @param jsonObject
	 * @param beanClass
	 * @return
	 * @throws IOException
	 * @throws JsonMappingException
	 * @throws JsonParseException
	 */
	public static <T> T toBean(String jsonObject, Class<T> beanClass) throws IOException {
		if (jsonObject == null || jsonObject.trim().equals("")) {
			return null;
		}
		ObjectMapper mapper = new ObjectMapper();
		// 过滤对象的null属性.
		mapper.setSerializationInclusion(JsonSerialize.Inclusion.NON_NULL);
		// 过滤map中的null值
		mapper.configure(SerializationConfig.Feature.WRITE_NULL_MAP_VALUES, false);
		mapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
		return mapper.readValue(jsonObject, beanClass);
	}

	/**
	 * 转换对象
	 * 
	 * @param jsonObject
	 * @param javaType
	 * @return
	 * @throws IOException
	 */
	public static Object toBean(String jsonObject, JavaType javaType) throws IOException {
		if (jsonObject == null || jsonObject.trim().equals("")) {
			return null;
		}
		ObjectMapper mapper = new ObjectMapper();
		// 过滤对象的null属性.
		mapper.setSerializationInclusion(JsonSerialize.Inclusion.NON_NULL);
		// 过滤map中的null值
		mapper.configure(SerializationConfig.Feature.WRITE_NULL_MAP_VALUES, false);
		mapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
		return mapper.readValue(jsonObject, javaType);
	}

	/**
	 * 获取组合类型 如：JavaType javaType = getCollectionType(ArrayList.class,
	 * YourBean.class);
	 * 
	 * @param collectionClass
	 * @param elementClasses
	 * @return
	 */
	public static JavaType getCollectionType(Class<?> collectionClass, Class<?>... elementClasses) {
		ObjectMapper mapper = new ObjectMapper();
		return mapper.getTypeFactory().constructParametricType(collectionClass, elementClasses);
	}
}
